<script lang="ts">
import { defineComponent, onUnmounted, reactive } from "vue";
import Header from './components/Header.vue';
import Footer from './components/Footer.vue';
import { pathMap, localstorage } from './utils/index';
import { useRouter } from 'vue-router';

export default defineComponent({
    name: 'App',
    components: {
      Header,
      Footer
    },
    setup() {
        const router = useRouter();

        const state = reactive({
          defaultOpen: ['1', '2'],
          showMenu: false,
          currentPath: '/brand'
        });

        if (window.history) {
          history.pushState(null, document.URL);
          window.addEventListener('popstate', () => {
            if (!localstorage.get('token')) {
              state.showMenu = false;
            }
          }, false)
        }

        const unwatch = router.beforeEach(( to, from, next ) => {
          if (to.path == '/login') {
              next();
          } else {
            if (!localstorage.get('token')) {
              next({ path: '/login' });
            } else {
              next();
            }
          }
          state.showMenu = to.path !== '/login';
          state.currentPath = to.path;
          document.title = pathMap[to.name as string];
        })

        onUnmounted(() => {
          unwatch();
        })

        return {
          state
        }
      }
  })
</script>

<template>
  <div class="layout">
    <el-container v-if="state.showMenu" class="container">
      <el-aside class="aside">
        <div class="head">
          <div>
            <img src="/src/assets/logo-manager.png" alt="logo">
            <span>同行车行管理系统</span>
          </div>
        </div>
        <div class="line"></div>
        <el-menu
          :default-openeds="state.defaultOpen"
          background-color="#222832"
          text-color="#fff"
          :router="true"
          :default-active="state.currentPath">
            <el-submenu index="1">
              <template #title>
                <span>模块管理</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="/brand">
                    <i class="el-icon-sell" />品牌管理
                </el-menu-item>
                <el-menu-item index="/leveltype">
                    <i class="el-icon-menu" />车辆类型管理
                </el-menu-item>
                <el-menu-item index="/pricetype">
                    <i class="el-icon-s-order" />价格区间管理
                </el-menu-item>
                <el-menu-item index="/enterprise">
                    <i class="el-icon-odometer" />企业管理
                </el-menu-item>
                 <el-menu-item index="/blackusers">
                    <i class="el-icon-user-solid" />用户黑名单管理
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
            <el-submenu index="2">
              <template #title>
                <span>个人中心</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="/user">
                    <i class="el-icon-thumb" />用户管理
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
        </el-menu>
      </el-aside>
      <el-container class="content">
        <Header />
        <div class="main">
          <router-view />
        </div>
        <Footer />
      </el-container>
    </el-container>
    <el-container v-else class="container">
      <router-view />
    </el-container>
  </div>
</template>

<style scoped>
  .layout {
    min-height: 100vh;
    background-color: #ffffff;
  }

  .container {
    height: 100vh;
  }

  .aside {
    width: 250px !important;
    background-color: #222832;
    overflow: hidden;
    overflow-y: auto;
  }

  .head {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;
  }
  .head > div {
    display: flex;
    align-items: center;
  }
  .head span {
    font-size: 20px;
    color: #ffffff;
  }

  .head img {
    width: 30px;
    height: 30px;
    margin-right: 10px;
  }

  .line {
    border-top: 1px solid rgba(107, 59, 59, 0.05);
    border-bottom: 1px solid rgba(0,0,0,.2);
  }

  .content {
    display: flex;
    flex-direction: column;
    max-height: 100vh;
    overflow: hidden;
  }

  .main {
    height: calc(100vh - 100px);
    overflow: auto;
    padding: 10px;
  }
</style>
